package com.jctrip.hotel.route.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jctrip.hotel.common.route2source.model.param.Route2SourceQueryOrderParam;
import com.jctrip.hotel.common.route2source.model.result.Route2SourceQueryOrderResult;
import com.jctrip.hotel.common.route2source.service.Route2SourceOrderService;
import com.jctrip.hotel.domain.dao.HtOrderSourceRecordDao;
import com.jctrip.hotel.domain.entity.HtOrderRouteRecord;
import com.jctrip.hotel.domain.entity.HtOrderSourceRecord;
import com.jctrip.hotel.domain.entity.HtRouteInfo;
import com.jctrip.hotel.domain.entity.HtSourceInfo;
import com.jctrip.hotel.route.common.ThreadLocalSession;
import com.jctrip.hotel.route.model.param.GetOrderInfoParam;
import com.jctrip.hotel.route.model.result.GetOrderInfoResult;

/**
 * 获取订单信息接口-服务
 */
@Service
public class GetOrderInfoService extends RouteService {

    @Autowired
    private HtOrderSourceRecordDao htOrderSourceRecordDao;

    @Autowired
    private Route2SourceOrderService route2SourceOrderService;

    public GetOrderInfoResult execute(GetOrderInfoParam param) {
        HtRouteInfo htRouteInfo = ThreadLocalSession.get().getRouteInfo();
        String routeId = htRouteInfo.getRouteId();
        HtOrderRouteRecord htOrderRouteRecord = param.getRouteOrderId() == null
                ? getOrderRouteRecord(param.getOrderId())
                : getOrderRouteRecord(routeId, param.getRouteOrderId());
        if (htOrderRouteRecord == null) {
            throw new RuntimeException("orderId或者routeOrderId无效");
        }
        if (!htOrderRouteRecord.getRouteId().equals(routeId)) {
            throw new RuntimeException("没有权限的订单");
        }
        HtOrderSourceRecord htOrderSourceRecord = getOrderSourceRecord(htOrderRouteRecord.getOrderId());
        if (htOrderSourceRecord == null) {
            throw new RuntimeException("没有供应下单记录");
        }
        HtSourceInfo htSourceInfo = getSourceInfo(htOrderSourceRecord.getSourceId());
        // 获取订单信息
        Route2SourceQueryOrderParam queryOrderParam = Route2SourceQueryOrderParam.builder()
                .sourceCode(htOrderSourceRecord.getSourceCode())
                .sourceAppId(htSourceInfo.getSourceAppId())
                .sourceSecret(htSourceInfo.getSourceSecret())
                .bookId(htOrderSourceRecord.getBookId())
                .sourceOrderId(htOrderSourceRecord.getSourceOrderId())
                .hotelId(htOrderSourceRecord.getHotelId())
                .extra(htSourceInfo.getExtra())
                .build();
        Route2SourceQueryOrderResult queryOrderResult = route2SourceOrderService.queryOrder(queryOrderParam);
        // 更新供应订单
        updateOrderSourceRecord(htOrderSourceRecord, queryOrderResult);
        return GetOrderInfoResult.builder()
                .routeOrderId(htOrderRouteRecord.getRouteOrderId())
                .orderId(htOrderRouteRecord.getOrderId())
                .hotelId(htOrderSourceRecord.getHotelId())
                .hotelName(htOrderSourceRecord.getHotelName())
                .roomTypeCode(htOrderSourceRecord.getRoomTypeCode())
                .roomTypeName(htOrderSourceRecord.getRoomTypeName())
                .orderStatus(queryOrderResult.getSourceOrderStatus())
                .arriveDate(htOrderRouteRecord.getArriveDate())
                .departDate(htOrderRouteRecord.getDepartDate())
                .breakfastCount(htOrderRouteRecord.getBreakfastCount())
                .roomCount(htOrderRouteRecord.getRoomCount())
                .contactName(htOrderRouteRecord.getContactName())
                .guestName(htOrderRouteRecord.getGuestName())
                .orderNotes(htOrderRouteRecord.getOrderNotes())
                .orderCreateTime(htOrderRouteRecord.getOrderCreateTime())
                .build();
    }

    private void updateOrderSourceRecord(
            HtOrderSourceRecord htOrderSourceRecord,
            Route2SourceQueryOrderResult queryOrderResult) {
        if (queryOrderResult.getSourceOrderStatus() != null) {
            htOrderSourceRecord.setSourceOrderStatus(queryOrderResult.getSourceOrderStatus());
        }
        htOrderSourceRecord.setOrderModifyTime(new Date());
        htOrderSourceRecordDao.updateById(htOrderSourceRecord);
    }
}
